package main

import (
	"fmt"
	"sync"
	"time"
)

var c chan struct{}
var c2 chan struct{}

func main() {
	c = make(chan struct{})
	c2 = make(chan struct{})

	wg := do(job1, job2, job3)
	wg.Wait()
}

func do(fs ...func()) *sync.WaitGroup {
	wg := &sync.WaitGroup{}

	for _, fn := range fs {
		wg.Add(1)
		go func(f func()) {
			defer wg.Done()
			f()
		}(fn)
	}

	return wg
}

func job1() {
	fmt.Println("job1")
	time.Sleep(2 * time.Second)
	//必须job1先执行了之后才能执行其他函数
	c <- struct{}{}
}
func job2() {
	<-c
	fmt.Println("job2")
	time.Sleep(2 * time.Second)

	//必须job2先执行了之后才能执行其他函数
	c2 <- struct{}{}
}
func job3() {
	<-c2
	fmt.Println("job3")
}
